//
//  MySingleViewController.m
//  AllFamily
//
//  Created by chirui on 2018/6/4.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "MySingleViewController.h"
#import "SingleTopUpViewController.h"

@interface MySingleViewController (){
    
    NSString * pay_pwdStr;
}
@property (weak, nonatomic) IBOutlet UILabel *pointLabel;
@property (weak, nonatomic) IBOutlet UITextField *regesPhoto;
@property (weak, nonatomic) IBOutlet UITextField *moneyText;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@end

@implementation MySingleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    TitleView(@"我要报单");
    
    self.sureBtn.layer.cornerRadius = 4.f;
    self.sureBtn.layer.masksToBounds = YES;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_renter" target:self action:@selector(backBtnClick)];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightItemWithTitle:@"充值" target:self action:@selector(rightBtnClick)];
    
    [self getUserMessageRequest];
    
    
    // 支付密码输入框
    self.verifyCodeInputView = [[VerifyCodeInputView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-SH_TopHeight)];
    self.verifyCodeInputView.inputView.delegate = self;
    [self.verifyCodeInputView setHidden:YES];
    [self.view addSubview:self.verifyCodeInputView];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)rightBtnClick{
    
    SingleTopUpViewController * sing = [[SingleTopUpViewController alloc]init];
    [self.navigationController pushViewController:sing animated:YES];
}


-(void)getUserMessageRequest{
    
    NSDictionary * dic = @{@"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token")};
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_singleMessage_api) andparameters:dic Success:^(id responseObject) {
        
        if (codeSucess) {
            
            NSString * point = responseObject[@"datas"][@"balance"];
            if (point.length<=0) {
                point = @"0";
            }
          _pointLabel.text = point;
            
        }else {
            
            if ([responseObject[@"code"] intValue]==220) {
                
                USER_DEFAULTSet(@"", @"token");
                USER_DEFAULTSet(@"no", @"enter");
                USER_DEFAULTSet(@"no", @"tokenVerify");
                USER_DEFAULTSet(@"", @"mid");
           
            }
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        
        SHuiMBhudTextTimeStr(@"网络错误，请稍后再试");
    }];
    
}


- (IBAction)sureBtnClick:(UIButton *)sender {
    
    if (_regesPhoto.text.length<=0) {
        
        SHuiMBhudTextTimeStr(@"请输入注册账号");
    }else if (_moneyText.text.length<=0){
        
        SHuiMBhudTextTimeStr(@"请输入充值金额");
    }else {
        
        [self checkPayPasswordSetting];
    }
    
}

-(void)serverRequestFor:(NSString *)str{
    
    NSDictionary * dic = @{@"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token"),
                           @"price":_moneyText.text,
                           @"phone":_regesPhoto.text,
                           @"pay_password":str
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(service_recharge_api) andparameters:dic Success:^(id responseObject) {
        
        if (codeSucess) {
            
            SHuiMBhudTextTimeStr(@"提交成功");
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else {
            
            if ([responseObject[@"code"] intValue]==220) {
                
                USER_DEFAULTSet(@"", @"token");
                USER_DEFAULTSet(@"no", @"enter");
                USER_DEFAULTSet(@"no", @"tokenVerify");
                USER_DEFAULTSet(@"", @"mid");
                
            }
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        
        SHuiMBhudTextTimeStr(@"网络错误，请稍后再试");
    }];
    
}

- (void)checkPayPasswordSetting {
  
    [self.verifyCodeInputView setHidden:NO];
    [self.verifyCodeInputView.inputView.textFieldArray[0] becomeFirstResponder];
}

#pragma mark - 支付密码输入完成回调
- (void)inputFinishedWithText:(NSString *)text {
    // text为用户输入的6位密码
    
    self.verifyCodeInputView.hidden = YES;
    pay_pwdStr = text;
    for(UITextField *textField in self.verifyCodeInputView.inputView.textFieldArray) {
        textField.text = @"";
    }
    
    if (pay_pwdStr.length!=6) {
        [SHJudgeTools MBProgressHUDPromptWith:@"支付密码格式不正确" On:self.view];
        return;
    }else {
        
        [self serverRequestFor:pay_pwdStr];
        
    }
    
}


@end
